package com.ytjj.qmyx.admin.service.impl;

import com.ytjj.qmyx.admin.dao.DynamicMatchingDao;
import com.ytjj.qmyx.admin.service.DynamicMatchingService;
import com.ytjj.qmyx.mall.mapper.DynamicMatchingMapper;
import com.ytjj.qmyx.mall.model.DynamicMatching;
import com.ytjj.qmyx.mall.model.DynamicMatchingExample;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DynamicMatchingServiceImpl implements DynamicMatchingService {

    @Resource
    private DynamicMatchingMapper dynamicMatchingMapper;

    @Resource
    private DynamicMatchingDao dynamicMatchingDao;

    @Override
    public DynamicMatching getDynamicMatching(Integer dynamicId) {
        DynamicMatchingExample dynamicMatchingExample = new DynamicMatchingExample();
        dynamicMatchingExample.createCriteria().andDynamicIdEqualTo(dynamicId);
        List<DynamicMatching> list = dynamicMatchingMapper.selectByExampleWithBLOBs(dynamicMatchingExample);
        if(!CollectionUtils.isEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int doDynamicMatching(DynamicMatching dynamicMatching) {
        // 修改或增加时，需要先判断是否存在改推荐信息记录
        DynamicMatchingExample dynamicMatchingExample = new DynamicMatchingExample();
        dynamicMatchingExample.createCriteria().andDynamicIdEqualTo(dynamicMatching.getDynamicId());
        List<DynamicMatching> list = dynamicMatchingMapper.selectByExampleWithBLOBs(dynamicMatchingExample);
        if(!CollectionUtils.isEmpty(list)){
            DynamicMatching am = list.get(0);
            dynamicMatching.setUpdateTime(new Date());
            return dynamicMatchingDao.updateDynamicMatching(dynamicMatching);
        }
        dynamicMatching.setCreateTime(new Date());
        return dynamicMatchingDao.insertDynamicMatching(dynamicMatching);
    }

}
